package crawl;

import crawl.cache.Cache;
import crawl.cache.RedisCache;
import crawl.cache.SimpleCache;
import org.htmlparser.Node;
import org.htmlparser.NodeFilter;
import org.htmlparser.Parser;
import org.htmlparser.filters.TagNameFilter;
import org.htmlparser.tags.LinkTag;
import org.htmlparser.util.NodeList;
import org.htmlparser.util.ParserException;

public class ContentParser {

    private int depth;

    private Cache cache = getCache();

    public ContentParser(int depth) {
        this.depth = depth;
    }

    public void doParse(String content) {
        try {
            Parser parser = new Parser(content);
            NodeFilter filter = new TagNameFilter("a");
            NodeList nodes = parser.extractAllNodesThatMatch(filter);
            if (nodes == null)
                return;

            for (int i = 0; i < nodes.size(); i++) {
                Node node = nodes.elementAt(i);
                if (node instanceof LinkTag) {
                    LinkTag link = (LinkTag) node;
                    if (link != null) {
                        String url = link.extractLink();
                        if (cache.exists(url))
                            continue;

                        UrlInfo urlInfo = getUrlInfo(link);
                        if (urlInfo != null) {
                            TaskManager.getInstance().putUrl(urlInfo);
                        }
                    }
                }
            }
        } catch (ParserException e) {
            // e.printStackTrace();
        }
    }

    private UrlInfo getUrlInfo(LinkTag link) {
        UrlInfo urlInfo = null;
        String url = link.extractLink();
        String[] ss = url.split(":");
        if (ss == null || ss.length != 2)
            return null;
        else {
            String host = ss[0];
            String path = ss[1];
            if (!Constants.whiteListPrefix.containsKey(host))
                return null;

            int pos = path.lastIndexOf("/");
            String endStr = path.substring(pos + 1, path.length());
            if (endStr == null)
                return null;

            if (!Constants.whiteListSuffix.containsKey(endStr))
                return null;

            urlInfo = new UrlInfo();
            if (host.equals("http"))
                urlInfo.setProtocal(0);
            else
                urlInfo.setProtocal(1);

            urlInfo.setHost(host);
            urlInfo.setPath(path);
            urlInfo.setUrl(url);
            return urlInfo;
        }
    }

    private Cache getCache() {
        // return SimpleCache.getInstance();
        return RedisCache.getInstance();
    }

}
